package com.atguigu.guli.service.edu.controller.admin;


import com.atguigu.guli.service.edu.entity.Course;
import com.atguigu.guli.service.edu.entity.CourseDescription;
import com.atguigu.guli.service.edu.entity.form.CourseListQueryAll;
import com.atguigu.guli.service.edu.entity.form.CoursePublishVo;
import com.atguigu.guli.service.edu.service.CourseDescriptionService;
import com.atguigu.guli.service.edu.service.CourseService;
import com.atguigu.guli.service.result.R;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2021-06-23
 */

@RestController
@RequestMapping("/edu/course")
public class AdminCourseController {
    @Autowired
    CourseService courseService;
    @Autowired
    CourseDescriptionService courseDescriptionService;

    @PutMapping("/getPub/{courseId}")
    public R getPub(@PathVariable String courseId){
        UpdateWrapper<Course> wrapper = new UpdateWrapper<>();
        wrapper.set("status", "normal");
        wrapper.set("publish_time", new Date());
        wrapper.eq("id",courseId);
        courseService.update(wrapper);
        return R.ok();
    }

    @GetMapping("/getCourseById/{courseId}")
    public R getCourseById(@PathVariable String courseId){
        CourseListQueryAll courseListQueryAll = courseService.getCourseById(courseId);
        return R.ok().data("item",courseListQueryAll);
    }

    @GetMapping("/getPageList/{pageNum}/{pageSize}")
    public R getPageList(@PathVariable Integer pageNum, @PathVariable Integer pageSize){

       Page<CourseListQueryAll> page = courseService.QueryPageOtherFrom(pageNum,pageSize);
        return R.ok().data("page",page);

    }

    @PostMapping("/add")  //添加数据
    public R add(@RequestBody CoursePublishVo coursePublishVo){
       String courseId = courseService.saveCourse(coursePublishVo);
        System.out.println(courseId);
        return R.ok().data("id",courseId);
    }

    @GetMapping("/sel/{id}")  //回显数据
    public R sel(@PathVariable String id){
        Course course = courseService.getById(id);
        CourseDescription courseDescription = courseDescriptionService.getById(id);

        CoursePublishVo coursePublishVo = new CoursePublishVo();

        //将course的数据给coursePublishVo
        BeanUtils.copyProperties(course, coursePublishVo);
        coursePublishVo.setDescription(courseDescription.getDescription());
        return R.ok().data("list",coursePublishVo);
    }

    @PutMapping("/edit/{id}")
    public R edit(@PathVariable String id,
                  @RequestBody CoursePublishVo coursePublishVo){

        Course course = new Course();
        BeanUtils.copyProperties(coursePublishVo, course);
        course.setId(id);

        //修改课程
        courseService.updateById(course);
        //修改简介
        CourseDescription courseDescription = new CourseDescription();
        courseDescription.setDescription(coursePublishVo.getDescription());
        courseDescription.setId(id);
        courseDescriptionService.updateById(courseDescription);

        return R.ok();
    }

}

